﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConfigurationTool
{
    public partial class ConfigurationTool : Form
    {
        Base64Help base64 = new Base64Help();
        string DB_Link = string.Empty;

        public ConfigurationTool()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            if (txt_DBIP.Text.Trim() == string.Empty)
            {
                MessageBox.Show("数据库地址不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txt_DBPort.Text.Trim() == string.Empty)
            {
                MessageBox.Show("数据库端口不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txt_UserID.Text.Trim() == string.Empty)
            {
                MessageBox.Show("数据库用户名不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txt_Password.Text.Trim() == string.Empty)
            {
                MessageBox.Show("数据库密码不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txt_ServiceName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("数据库实例名不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            DB_Link = string.Format("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1}))(CONNECT_DATA=(SERVICE_NAME={2})));User ID={3};Password={4}",
                txt_DBIP.Text.Trim(), txt_DBPort.Text.Trim(), txt_ServiceName.Text.Trim(), txt_UserID.Text.Trim(), txt_Password.Text.Trim());

            var item = new
            {
                FtpServerIP = Readjson("FtpServerIP"),
                FtpUserID = Readjson("FtpUserID"),
                FtpPassword = Readjson("FtpPassword"),
                link_config = base64.EncodeBase64(DB_Link)
            };
            Writejson(System.Environment.CurrentDirectory + "\\config\\FtpConfig.json", JsonConvert.SerializeObject(item));
            MessageBox.Show("配置成功！");
        }       
        
        private void ConfigurationTool_Load(object sender, EventArgs e)
        {
            txt_ftpip.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txt_ftpip.Text.Trim() == string.Empty)
            {
                MessageBox.Show("FTP地址不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txt_ftpName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("FTP用户名不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txt_ftpPassWord.Text.Trim() == string.Empty)
            {
                MessageBox.Show("FTP密码不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var item = new
            {
                FtpServerIP = txt_ftpip.Text.Trim(),
                FtpUserID = txt_ftpName.Text.Trim(),
                FtpPassword = txt_ftpPassWord.Text.Trim(),
                link_config = Readjson("link_config")
            };
            Writejson(System.Environment.CurrentDirectory + "\\config\\FtpConfig.json", JsonConvert.SerializeObject(item));
            MessageBox.Show("配置成功！");
        }

        public static string Readjson(string key)
        {
            string strPath = System.Environment.CurrentDirectory + "\\config";
            string jsonfile = strPath + "\\FtpConfig.json";//JSON文件路径
            if (!Directory.Exists(strPath))
            {
                //model.code = "-4";
                //model.msg = "医保配置文件不存在 !" + jsonfile;
            }

            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    var value = o[key].ToString();
                    return value;
                }
            }
        }

        public static void Writejson(string path, string json)
        {
            File.WriteAllText(path, json, System.Text.Encoding.UTF8);
        }
    }
}
